<?php

namespace App\Exceptions\API;

use Illuminate\Support\MessageBag;
use Dingo\Api\Contract\Debug\MessageBagErrors;

class ValidationFailed extends APIException implements MessageBagErrors
{
    const STATUS_CODE = 422;

    const CODE = 4422;

    const MESSAGE = 'validation_failed';

    /**
     * MessageBag errors.
     *
     * @var \Illuminate\Support\MessageBag
     */
    protected $errors;

    /**
     * ValidationFailed constructor.
     * @param string $description
     * @param null $errors
     * @param \Exception|null $previous
     */
    public function __construct($description, $errors = null, \Exception $previous = null)
    {
        if (is_null($errors)) {
            $this->errors = new MessageBag;
        } else {
            $this->errors = is_array($errors) ? new MessageBag($errors) : $errors;
        }
        parent::__construct($description, $previous);
    }

    /**
     * Get the errors message bag.
     *
     * @return \Illuminate\Support\MessageBag
     */
    public function getErrors()
    {
        return $this->errors;
    }

    /**
     * Determine if message bag has any errors.
     *
     * @return bool
     */
    public function hasErrors()
    {
        return ! $this->errors->isEmpty();
    }
}
